<?php 
namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class FormDictModel extends Model
{
    const STATUS_ACTIVE = 1;
    const STATUS_DELETE = 0;

    const DEFAULT_KEY = array('1','2','3');

    protected $connection = 'db_main';
    protected $table = 'wp_sdk_form_dict';

    // protected $fillable = ['appid', 'item_key', 'item_value', 'status', 'create_time', 'modify_time'];

    public $timestamps = false;

    public static function getAllItems($uid = 0)
    {
        $whereRaw = $uid == 0 ? 'is_default = 1 and status =' .self::STATUS_ACTIVE : '(is_default = 1 or uid='.$uid.') AND status='.self::STATUS_ACTIVE;
        $rows = self::whereRaw($whereRaw)->get();
        $ret = array();
        if($rows) {
            foreach ($rows as $key => $row) {
                $ret[$key] = array(
                    'id'  => $row->id,
                    'name'  => $row->name,
                    'label' => $row->label,
                    'type'  => $row->type,
                    'rule'  => $row->rule,
                    'style'  => $row->is_default ? 'default' : 'custom'
                );
            }
        }
        unset($rows);
        return $ret;
    }

}
